package assistant

import (
	"context"

	"paper-pro/app/assistant/api/internal/svc"
	"paper-pro/app/assistant/api/internal/types"
	"paper-pro/pkg/ctxdata"
	assistantclient "paper-pro/pkg/rpc/assistant"

	"github.com/pkg/errors"
	"github.com/zeromicro/go-zero/core/logx"
)

type DeleteLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// delete assistant
func NewDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteLogic {
	return &DeleteLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *DeleteLogic) Delete(req *types.IdReq) (resp *types.AssistantMeta, err error) {
	userId, err := ctxdata.GetUidFromCtx(l.ctx)
	if err != nil {
		return nil, errors.Wrapf(err, "Get Assistant: get UserId error: %v", err)
	}
	rpcResp, err := l.svcCtx.AssistantRpc.DeleteAssistant(l.ctx, &assistantclient.DeleteAssistantReq{
		Id:     req.Id,
		UserId: userId,
	})
	if err != nil {
		return nil, errors.Wrapf(err, "Delete Assistant: AssistantRpc DeleteAssistant error: %v", err)
	}

	return &types.AssistantMeta{
		Id:        rpcResp.AssistantMeta.Id,
		Name:      rpcResp.AssistantMeta.Name,
		LastUseAt: rpcResp.AssistantMeta.LastUseAt,
	}, nil
}
